package com.cuckoo.mall.thus.menuaccount.setting;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bgn.baseframe.base.BaseActivity;
import com.bgn.baseframe.network.HttpObserver;
import com.bgn.baseframe.network.bean.TDataBean;
import com.bgn.baseframe.utils.ToastUtil;
import com.bgn.baseframe.utils.evenbus.MainEvent;
import com.bgn.baseframe.utils.imageloader.GlideCircleTransform;
import com.bgn.baseframe.view.picture.SelectPictureActivity;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.request.RequestOptions;
import com.cuckoo.mall.R;
import com.cuckoo.mall.thus.manager.UserInfoManager;
import com.cuckoo.mall.javabean.UrlBean;
import com.cuckoo.mall.network.ApiManager;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


@Route(path = "/mall/settingActivity", group = "mall")
public class SettingActivity extends BaseActivity {


    @BindView(R.id.iv_image)
    ImageView ivImage;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_logout)
    TextView tvLogout;

    @BindView(R.id.lly_name)
    RelativeLayout llyName;
    @BindView(R.id.lly_image)
    LinearLayout llyImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        getTitleBar().setTitle(getString(R.string.setting));
        if (TextUtils.isEmpty(UserInfoManager.getInstance().getUserInfo().getData().getNick_name())) {
            tvName.setText(UserInfoManager.getInstance().getUserInfo().getData().getEmail());
        } else {
            tvName.setText(UserInfoManager.getInstance().getUserInfo().getData().getNick_name());
        }
        Glide.with(getActivity()).load(UserInfoManager.getInstance().getUserInfo().getData().getHead_img()).apply(new RequestOptions().priority(Priority.IMMEDIATE).transform(new GlideCircleTransform(getActivity())).placeholder(R.mipmap.head).error(R.mipmap.head)).into(ivImage);

    }

    @OnClick({R.id.lly_image, R.id.tv_name, R.id.lly_name, R.id.iv_image, R.id.tv_logout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_name:
            case R.id.lly_name:
                if (UserInfoManager.getInstance().isLogin() && UserInfoManager.getInstance().getUserInfo().getData() != null) {
                    ARouter.getInstance().build("/mall/settingNameActivity").navigation(this, 100);
                } else {
                    ToastUtil.showInfo("error:no user data");
                }
                break;
            case R.id.iv_image:
            case R.id.lly_image:
                Intent intent = new Intent(getActivity(), SelectPictureActivity.class);
                startActivityForResult(intent, 100);
                break;
            case R.id.tv_logout:
                UserInfoManager.getInstance().exitLogin();
                //EventBus.getDefault().post(new MainEvent(MainEvent.HIDE_CART_WHEN_LOGIN_OUT));
                // finish();
                ARouter.getInstance().build("/mall/mainActivity").withString("action", "home").navigation();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 100 && resultCode == RESULT_OK) {
            tvName.setText(UserInfoManager.getInstance().getUserInfo().getData().getNick_name());
        }

        if (requestCode == requestCode && requestCode == 100 && data != null) {
            String path = data.getStringExtra("path");
            //  Glide.with(this).load(path).into(ivImage);
            if (isNetworkerConnectHint()) {
                showLoadingView();
                ApiManager.uploadHeadImage(new File(path)).subscribe(new HttpObserver<TDataBean<UrlBean>>(this) {
                    @Override
                    public void onSucceed(TDataBean<UrlBean> value) {
                        if (value.getData() != null) {
                            Glide.with(getActivity()).load(value.getData().getUrl()).apply(new RequestOptions().priority(Priority.IMMEDIATE).transform(new GlideCircleTransform(getActivity())).placeholder(R.mipmap.head).error(R.mipmap.head)).into(ivImage);
                            UserInfoManager.getInstance().getUserInfo().getData().setHead_img(value.getData().getUrl());
                        } else {
                            ToastUtil.showInfo("error");
                        }
                    }
                });
            }
        }
    }
}
